<?php
namespace app\route;
use think\Cache;
use think\Log;
use think\Route;
use think\Config;
use think\DB;
use think\Request;

// 判断入口，来决定采用
MIP_HOST ? config('assets', 'public/assets') : config('assets', 'assets');

// 判断是否已经成功安裝了
if (!is_file(APP_PATH. 'install' . DS .'install.lock')) {
    Route::rule('bch-install.php','install/BchInstall/index');
    Route::rule('/','install/Install/index');
    Route::rule('/install','install/Install/index');
    Route::rule('/install/installPost','install/Install/installPost');
    Route::rule('/install/installPostOne','install/Install/installPostOne');

} else {
    $request = Request::instance();

    // 获取网站 根地址
    $domain = $request->domain() ;

    // 默认domain是带着端口的，所以要去掉http的80端口  和 https 的 443端口
    $lenDomain = strlen($domain);
    if( strpos($domain,'http://')===0 && substr($domain,$lenDomain-3)===':80'){
        $domain = substr($domain,0,$lenDomain-3);
    }else if( strpos($domain,'https://')===0 && substr($domain,$lenDomain-4)===':443'){
        $domain = substr($domain,0,$lenDomain-4);
    }

    $mipInfo['app_debug'] = config('app_debug') ;

    // 获得settings表中所有的数据，并组成key =>value 的mipInfo

    $settings = model('app\setting\model\Setting')->getList();
    foreach ($settings as $k => $v) {
        $mipInfo[$v['key']] = $v['val'];
    }

    if(!config('app_debug') ){
        $mipInfo['scriptVersion']=config('scriptVersion').'-'.date("Ymd",time());
    }else{
        $mipInfo['scriptVersion']=time();
    }

    // 获得模板名称
    $tplName = null;

    if(isset($mipInfo['template'])){
        $tplName = $mipInfo['template'];
    }
    $tplName = $tplName ? $tplName : 'default';
    $mipInfo['template']= $tplName;

    // 网站域名信息，也写入mipInfo
    $mipInfo['domain'] = $domain;

    if(empty($mipInfo['titleSeparator'])){
        $mipInfo['titleSeparator'] = ' - ';
    }

    config('mipInfo',$mipInfo);

	$pages = [];
    $template = config('template.view_path') . $tplName . DS;
    if (is_dir($template)) {
        $templateFile = opendir($template);
        if ($templateFile) {
            while (false !== ($file = readdir($templateFile))) {
                if (substr($file, 0, 1) != '.' AND is_dir($template . DS . $file)) {
                    $pages[] = $file;
                }
            }
            closedir($templateFile);
        }
    }

	$subHtml = '';
	if ($pages) {
		foreach ($pages as $key => $value) {
			if (strpos($value, 'html') !== false) {
				$subHtml = $value . DS;
			}
		}
	}
    config('template.view_path',config('template.view_path') . $tplName . DS . $subHtml);
    config('admin','admin'); //如果修改系统管理地址，请修改后一个admin即可

    foreach (fetch_file_lists(ROOT_PATH . 'addons' . DS) as $key => $file) {
        if (strstr($file,'route.php')) {
            require $file;
        }
    }

    // 如果是Api接口
    if ( strpos($request->url(),'/Api') !== false ) {

    }else{
        if ( strpos($request->url(),'/admin') === 0 ) {
            // 配置admin的 路由
            Route::group(Config::get('admin'), [

                ':model/:action/:addonsCtr/:addonsAct/[:params]' => 'admin/Admin/index?model=:model&action=:action&params=:params&addonsAct=:addonsAct&addonsCtr=:addonsCtr',

                ':model/:action/:params' => 'admin/Admin/index?model=:model&action=:action&params=:params',

                ':model/:action/' => 'admin/Admin/index?model=:model&?action=:action',

                ':model' => 'admin/Admin/index?model=:model',

                '/' => 'admin/Admin/index',

            ], [], []);

        }else {

            Route::group('Tag', [

                '[:id]/index' . config('urlPageBreak') . '<page>' => ['tag/Tag/tagDetail', ['ext' => 'html'], ['id' => '^[a-zA-Z0-9_-]+'], ['page' => '\d+']],

                ':id' => ['tag/Tag/tagDetail', [], ['id' => '^[a-zA-Z0-9_-]+']],

            ], [], []);

        }
    
    }

    foreach (fetch_file_lists(APP_PATH) as $key => $file) {
        if (strstr($file,'route.php')) {
            require_once $file;
        }
    }

}